from flask import Flask, g, current_app, jsonify
from flask_httpauth import HTTPTokenAuth
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer



app = Flask(__name__)
auth = HTTPTokenAuth(scheme='Token')
app.config['SECRET_KEY'] = 'weex'

tokens = {
    "secret-token-1": "john",
    "secret-token-2": "susan"
}

#check_password_hash


#生成Token
def genrate_outh_toekn(uid, type):
    s = Serializer(current_app.config['SECRET_KEY'], expires_in = 7200)
    return s.dumps({'uer': uid,
                    'type': type})



@app.route('/gettoken')
def get_token():
    token = genrate_outh_toekn('john', 'web')
    data = {
        'token': token.decode('ascii')
    }
    return jsonify(data), 201




#校验token
# @auth.verify_token
# def verify_token(token):
#     if token in tokens:
#         g.current_user = tokens[token]
#         return True
#     return False

@auth.verify_token
def verify_token(token):
    s = Serializer(current_app.config['SECRET_KEY'])
    data = s.loads(token)
    print(data)
    return True


@app.route('/')
@auth.login_required
def index():
    return "Hello, %s!"

if __name__ == '__main__':
    app.run()
    #print(get_token())

#verify
#Authorization Token secret-token-1